
//这个类用于对数据库中博客数据的修改

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BlogDao {
    //1.博客列表页查询博客列表
    public List<Blog> selectAll() {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<Blog> blogList = new ArrayList<>();
        try {
            //建立连接
            connection = DBUtil.getConnection();
            //构造sql(注意这里按照时间降序排序)
            String sql = "select * from blog order by postTime desc";
            statement = connection.prepareStatement(sql);
            //执行sql
            resultSet = statement.executeQuery();
            while(resultSet.next()) {
                Blog blog = new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                //博客列表页这里正文不适合太长，超出范围，省略即可
                String content = resultSet.getString("content");
                if(content.length() > 100) {
                    content = content.substring(0, 100) + "...";
                }
                blog.setContent(content);
                blog.setUserId(resultSet.getInt("userId"));
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                blogList.add(blog);
            }
        } catch(SQLException e) {
            e.printStackTrace();
        } finally {
            //释放资源
            DBUtil.close(connection, statement, resultSet);
        }
        return blogList;
    }
    //2.根据博客ID查询指定博客
    public Blog selectOne(int blogId) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            //建立连接
            connection = DBUtil.getConnection();
            //构造sql
            String sql = "select * from blog where blogId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, blogId);
            //执行sql
            resultSet = statement.executeQuery();
            if(resultSet.next()) {
                Blog blog = new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                blog.setContent(resultSet.getString("content"));
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                blog.setUserId(resultSet.getInt("userId"));
                return blog;
            }
        } catch(SQLException e) {
            e.printStackTrace();
        } finally {
            //释放资源
            DBUtil.close(connection, statement, resultSet);
        }
        return null;
    }
    //3.插入博客 —— 发布博客
    public void insert(Blog blog) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            //建立连接
            connection = DBUtil.getConnection();
            //构造sql
            String sql = "insert into blog values(null, ?, ?, now(), ?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1, blog.getTitle());
            statement.setString(2, blog.getContent());
            statement.setInt(3, blog.getUserId());
            //执行sql
            int ret = statement.executeUpdate();
            if(ret != 1) {
                System.out.println("博客插入失败!");
            }else {
                System.out.println("博客插入成功!");
            }
        } catch(SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection, statement, null);
        }
    }
    //4.根据博客Id删除博客
    public void delete(int blogId) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            //建立连接
            connection = DBUtil.getConnection();
            //构造sql
            String sql = "delete from blog where blogId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, blogId);
            //执行sql
            int ret = statement.executeUpdate();
            if(ret != 1) {
                System.out.println("博客删除失败!");
            }else {
                System.out.println("博客删除成功!");
            }
        } catch(SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection, statement, null);
        }
    }
}
